#include "commonpage.h"
#include "ui_commonpage.h"
#include <QDebug>
#include "listitembox.h"
#include "musiclist.h"

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    //关闭横向滚动条
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    //播放全部按钮点击后,发送playAll信号,让MusicPlayer播放对应页面歌曲
    connect(ui->playAllBtn, &QPushButton::clicked, this, [=](){
        emit playAll(pageType);
    });

    //当页面中的歌曲项被双击之后发送信号,让MusicPlayer播放对应页面的index歌曲
    connect(ui->pageMusicList, &QListWidget::doubleClicked, this, [=](const QModelIndex &index){
        emit playMusicByIndex(this, index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setMusicListType(PageType pageType)
{
    this->pageType = pageType;
}

void CommonPage::setCommonPageUi(const QString &imagePath, const QString &text)
{
    //设置文本
    ui->pageTittle->setText(text);

    //设置图片
    ui->musicImageLabel->setPixmap(imagePath);
    //自适应缩放
    ui->musicImageLabel->setScaledContents(true);

}

void CommonPage::addMuiscToPage(MusicList &musicList)
{
    //清空之前数据
    musicOfPage.clear();

    for(auto e : musicList)
    {
        switch (pageType)
        {
            case LIKE_PAGE:
            if(e.getIsLike())
            {
                musicOfPage.push_back(e.getMusicId());
            }
            break;
            case LOCAL_PAGE:
            musicOfPage.push_back(e.getMusicId());
            break;
            case HISTORY_PAGE:
            if(e.getIsHistory())
            {
                musicOfPage.push_back(e.getMusicId());
            }
            break;
            default:
            qDebug() << "尚未支持";
        }
    }

}

void CommonPage::addMusicToPlaylist(MusicList &musicList, QMediaPlaylist *playlist)
{
    //清空之前数据
    playlist->clear();

    for(auto e : musicList)
    {
        switch (pageType)
        {
            case LIKE_PAGE:
            if(e.getIsLike())
            {
                playlist->addMedia(e.getMusicUrl());
            }
            break;
            case LOCAL_PAGE:
            playlist->addMedia(e.getMusicUrl());
            break;
            case HISTORY_PAGE:
            if(e.getIsHistory())
            {
                playlist->addMedia(e.getMusicUrl());
            }
            break;
            default:
            qDebug() << "尚未支持";
        }
    }
}

QString CommonPage::getMusicIdByIndex(int index)
{
    if(index >= musicOfPage.size())
    {
        qDebug() << "无此歌曲";
        return "";
    }

    return musicOfPage[index];
}

void CommonPage::reFresh(MusicList &musicList)
{
    //清空pageMusicList,避免重复添加
    ui->pageMusicList->clear();

    addMuiscToPage(musicList);

    for(auto e : musicOfPage)
    {
        auto it = musicList.findMusicById(e);
        if(it == musicList.end())
            continue;

        //添加歌曲项(需要通过setItemWidget方法将ListItemBox绑定到QListWidgetItem)
        //初始化音乐信息
        ListItemBox *listItem = new ListItemBox(this);
        listItem->setMusicName(it->getMusicName());
        listItem->setMusicSinger(it->getMusicSinger());
        listItem->setMusicAlbum(it->getMusicAlbumn());
        listItem->setLikeMusic(it->getIsLike());
        QListWidgetItem *item = new QListWidgetItem(ui->pageMusicList);
        item->setSizeHint(QSize(listItem->width(), listItem->height()));
        ui->pageMusicList->setItemWidget(item, listItem);

        connect(listItem, &ListItemBox::isLikeMusic, this, [=](bool isLike){
            //发出信号updataLikeMusic,让MusicPlayer去更新localPage,likePage,recentPage
            emit updateLikeMusic(isLike, it->getMusicId());
        });
    }

    //重绘窗口paintEvent
    //update()将重绘事件加入事件循环,不会立即执行, repaint()会立即重绘
    repaint();
}
